 package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;

import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.webserver.WebServer;

import services.HealthSystemServicesProvider;

@SuppressWarnings("serial")
public class HealthSystemView extends JFrame{
	private JLabel label;
	private JButton button;
	private WebServer webServer;
	
	public HealthSystemView(WebServer webServer){
		super("Health System Services Provider");
		this.webServer = webServer;
		label = new JLabel("Click the button below to start/stop Health System service");
		label.setBounds(20, 10, 350, 25);
		button = new JButton("Start");
		button.addActionListener(startListener);
		button.setBounds(150, 45, 75, 25);
		this.setLayout(null);
		this.add(button);
		this.add(label);
		this.setVisible(true);
		this.setSize(385, 125);
		this.setLocationRelativeTo(null);
		addWindowListener(closeListener);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	WindowListener closeListener = new WindowListener() {
		@Override
		public void windowOpened(WindowEvent e) {
			
		}
		
		@Override
		public void windowIconified(WindowEvent e) {
			HealthSystemView.this.setVisible(false);
		}
		
		@Override
		public void windowDeiconified(WindowEvent e) {
			
		}
		
		@Override
		public void windowDeactivated(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void windowClosing(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void windowClosed(WindowEvent e) {
			if (button.getText().equals("Stop"))
				webServer.shutdown();
		}
		
		@Override
		public void windowActivated(WindowEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	};
	ActionListener startListener = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				webServer.start();
				System.out.println("Health System Server is started!");
				button.setText("Stop");
				button.removeActionListener(startListener);
				button.addActionListener(stopListener);
			} catch (IOException e1) {
				e1.printStackTrace();
				System.out.println("Health System Server can't start!");
			}
		}
	};
	
	ActionListener stopListener = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			webServer.shutdown();
			System.out.println("Health System Server is stopped!");
			button.setText("Start");
			button.removeActionListener(stopListener);
			button.addActionListener(startListener);
		}
	};
	
	public static void main(String[] args) {
		try {
			WebServer webServer = new WebServer(8989);
			XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
			PropertyHandlerMapping phm = new PropertyHandlerMapping();
			phm.addHandler("HealthSystem", HealthSystemServicesProvider.class);
			
			xmlRpcServer.setHandlerMapping(phm);
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			new HealthSystemView(webServer);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
